package com.bigknow.appstore.proxy.log.impl;

import com.bigknow.appstore.proxy.entity.ProxyLog;
import com.bigknow.appstore.proxy.enumerate.LogLevel;
import com.bigknow.appstore.proxy.log.ILogWriter;
import com.bigknow.appstore.web.base.entity.User;
import com.bigknow.appstore.web.console.app.Application;
import com.bigknow.appstore.web.framework.entity.IEntity;
import org.mongodb.morphia.Datastore;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.bigknow.appstore.common.Assert.assertNotNull;

/**
 * 将日志写入mongodb的类
 * Created by chaos on 2016/9/13
 * 2016-10-19 chaos 加入resName值的设置
 */
public class MongoDBWriter extends BaseWriter implements ILogWriter {

    private ExecutorService executorService;

    private Datastore ds;

    private LogLevel logLevel;

/*    private static MongoDBWriter instance;

    public static MongoDBWriter getInstance(){
        synchronized (MongoDBWriter.class){
            if(instance==null) instance = new MongoDBWriter();
        }
        return instance;
    }*/

    public MongoDBWriter(Datastore ds) {
        this.executorService = Executors.newFixedThreadPool(1);
        this.ds = ds;
    }

    public MongoDBWriter setDs(Datastore ds) {
        this.ds = ds;
        return this;
    }

    @Override
    public boolean isDebug() {
        return logLevel.equals(LogLevel.DEBUG);
    }

    @Override
    public boolean isInfo() {
        return logLevel.equals(LogLevel.INFO);
    }

    @Override
    public boolean isError() {
        return logLevel.equals(LogLevel.ERROR);
    }

    @Override
    public void setLevel(LogLevel logLevel) {
        this.logLevel = logLevel;
    }

    @Override
    public LogLevel getLevel() {
        return logLevel;
    }

    @Override
    public void writeLog(ProxyLog log) {
        assertNotNull(ds);
        executorService.execute(new Task(ds, log));
    }

    class Task implements Runnable {

        private Datastore ds;

        private IEntity entity;

        public Task(Datastore ds, IEntity entity) {
            this.ds = ds;
            this.entity = entity;
        }

        @Override
        public void run() {
            if (entity instanceof ProxyLog) {
                ProxyLog log = ((ProxyLog) entity);
                //设置资源名称
                if (log.getProxyResId() != null) {
                    Application app = ds.find(Application.class).retrievedFields(true, "name")
                            .field("id").equal(log.getProxyResId()).get();
                    if (app != null) {
                        log.setResName(app.getName());
                    }
                }
                //设置用户名称
                if (log.getUserId() != null) {
                    User user = ds.find(User.class).retrievedFields(true, "account")
                            .field("id").equal(log.getUserId()).get();
                    if (user!=null&&user.getAccount() != null) {
                        log.setUserName(user.getAccount());
                    }
                }
                //设置默认的调用状态中文显示名称
                log.setLogTypeNameByType();
            }
            ds.save(entity);
        }
    }
}
